import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import Users from '../views/users/Users.vue'
import Power from '../views/power/Power.vue'
import Roles from '../views/power/Roles.vue'

Vue.use(VueRouter)

const routes = [
  {path:'/',redirect:'/login'},
  {path:'/login',component:Login},
  {path:'/home',redirect:'/welcome',component:Home,children:[
    {path:'/welcome',component:Welcome},
    {path:'/users',component:Users},
    {path:'/rights',component:Power},
    {path:'/roles',component:Roles},
  ]}
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  if(to.path === '/login') return next();
  const tokenStr = sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')

  next()
})

export default router
